<?php

/**
 * @author  PVer
 * @date    2018-5-7 11:14:17
 * @version V1.0
 * @desc    
 */

namespace app\daikuan\controller;

use cmf\controller\AdminBaseController;
use think\Validate;
use app\daikuan\model\LogisticsModel;
use app\user\model\UserModel;
use app\daikuan\model\RoleUserModel;
use app\daikuan\model\CreditlevelModel;
use think\Db;

class LogisticsController extends AdminBaseController {

    public function index() {
        $map = array();
        $like_map = array();

        //        table排序
        $order = array();
        $order_type = input('order','desc') ;
        $order_name = input('order_name','a.id') ;
        if (empty($order_name)) {
            $order_name = 'a.id';
        }
        $order = "$order_name $order_type";


        $name = input('name');
        if (!empty($name)) {
            $condition = "(a.user_nickname like '%$name%' or a.mobile like '%$name%' or a.id like '%$name%') ";
            $map['a.user_nickname|a.mobile|a.id'] = ['like', '%' . $name . '%'];
        }
        $map['user_category'] = 2;
        $dk_status = input('dk_status', -1, 'intval');
        if ($dk_status >= 0) {
            $map['a.dk_status'] = $dk_status;
        }
        $list = db('user a ')->join(array(array('dk_logistics b', 'a.id=b.UserId', 'LEFT')
                    , array('dk_creditlevel c', 'b.CreditLevel=c.id', 'LEFT')
                ))
                ->field('a.user_login,a.user_nickname,a.avatar,a.birthday,a.create_time,a.id,a.logistics,a.mobile,b.LogisticsName,b.* ,b.BusLis Img1,a.dk_status,c.level_name')->order($order)
                ->where($map)
                ->paginate(10, false, ['query' => request()->param(),]);

        $page = $list->render();
        $this->assign('dk_status', $dk_status);
        $this->assign('order', $order_type);
        $this->assign('list', $list);
        $this->assign('page', $page);
        return $this->fetch();
    }

    public function add() {

        $map = array();
        $creditlevelModel = new CreditlevelModel();
        $creditlevel = $creditlevelModel->select();
        $this->assign("levellist", $creditlevel);
        return $this->fetch();
    }

    public function edit(){
        $id = input('id');
        $info = model('Logistics')->get($id);
        if (empty($info)) {
            $this->error("数据错误");
        }
        $user=Db::name('user')->where('Id',$info['UserId'])->find();
        $creditlevelModel = new CreditlevelModel();
        $creditlevel = $creditlevelModel->select();
        $this->assign("levellist", $creditlevel);
        $this->assign('info', $info);
        return $this->fetch();
    }

    public function audit() {
        $id = input('id');
        $creditlevelModel = new CreditlevelModel();
        $creditlevel = $creditlevelModel->select();
        $this->assign("levellist", $creditlevel);
        $map = array();
        $map["a.id"] = $id;
        $info = db('user a ')->join(array(array('dk_logistics b', 'a.id=b.UserId', 'LEFT')
                ))
                ->field(' a.user_nickname,a.avatar,a.birthday,a.create_time,a.id,a.logistics,a.mobile,b.LogisticsName,b.* ,b.BusLis Img1')
                ->where($map)
                ->find();

        if (empty($info)) {
            $this->error("数据错误");
        }
        $this->assign('info', $info);
        return $this->fetch();
    }

    public function modifypass() {
        $id = input('id');
        $this->assign('id', $id);
        return $this->fetch();
    }

    public function modifypassPost() {

        if ($this->request->isPost()) {
            $data = $this->request->post();

            $userModel = new UserModel();
            $user = array();
            $user['user_pass'] = cmf_password($data['user_pass']);
            $result = $userModel->save($user, 'id=' . $data['id']);
            if ($result !== false) {
                $this->success("操作成功", url("index"));
            } else {
                $this->error("操作失败");
            }
        } else {
            $this->error("数据错误");
            return $this->fetch('index');
        }
    }

    public function editPost() {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            //修改身份证
            $logisticsModel = new LogisticsModel();
            $arr1 = array();
            $arr1['LogisticsName'] = $data['LogisticsName'];
            $arr1['LogisticsType'] = $data['LogisticsType'];
            $arr1['Legal'] = $data['Legal'];
            $arr1['LegalPhone'] = $data['LegalPhone'];
            $arr1['RegistAmount'] = $data['RegistAmount'];
            $arr1['CreateDate'] = $data['CreateDate'];
            $arr1['WorkDays'] = $data['WorkDays'];
            $arr1['CreditLevel'] = $data['CreditLevel'];
            $arr1['CanLoanAmount'] = $data['CanLoanAmount'];
            $arr1['HaveAmount'] = $data['HaveAmount'];
            $arr1['BusLis'] = $data['imgid1'];
            $arr1['LastDate'] = date('Y-m-d H:i:s', time());;
            $result_1 = $logisticsModel->save($arr1, 'Id=' . $data['Id']);
            $logis = $logisticsModel->where('Id',$data['Id'])->find();

            //$up_data['user_level'] = $data['CreditLevel'];
            //Db::name('user')->where('Id',$logis['UserId'])->update($up_data);
            if ($result_1 !== false) {
                $this->success("操作成功", url("index"));
            } else {
                $this->error("操作失败");
            }
        } else {
            $this->error("请求错误");
            return $this->fetch(':index');
        }
    }

    public function addPost() {
        if ($this->request->isPost()) {
            $validate = new Validate([
                'user_login' => 'require',
                'user_pass' => 'require|min:6|max:12',
                'LogisticsName' => 'require',
            ]);
            $validate->message([
                'user_login.require' => '用户名不能为空',
                'user_pass.require' => '密码不能为空',
                'user_pass.min' => '密码长度不能小于6',
                'user_pass.max' => '密码长度最大12',
                'LogisticsName.require' => '公司名称不能为空'
            ]);
            $data = $this->request->post();
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }
//修改身份证
            $userModel = new UserModel();
            $user = array();
            $user['user_login'] = $data['user_login'];
            $user['user_pass'] = cmf_password($data['user_pass']);
            $user['user_nickname'] = $data['LogisticsName'];
            $user['mobile'] = $data['LegalPhone'];
            $user['user_category'] = 2;
            $user['user_type'] = 3;
            $user['last_login_time'] = strtotime('now');
            $user['create_time'] = strtotime('now');
            $isHave = $userModel->checkLogCode($user);
            if ($isHave == 1) {
                $this->error("用户名已存在");
                exit();
            }
            if ($isHave == 2) {
                $this->error("该物流公司已经存在");
                exit();
            }
            $result_1 = $userModel->save($user);
            if ($result_1 > 0) {
                $roleUser = new RoleUserModel();
                $role = array();
                $role['role_id'] = 3;
                $role['user_id'] = $userModel->id;
                $roleUser->save($role);

                $logisticsModel = new LogisticsModel();
                $arr1 = array();
                $arr1['UserId'] = $userModel->id;
                $arr1['LogisticsName'] = $data['LogisticsName'];
                $arr1['LogisticsType'] = $data['LogisticsType'];
                $arr1['Legal'] = $data['Legal'];
                $arr1['LegalPhone'] = $data['LegalPhone'];
                $arr1['RegistAmount'] = $data['RegistAmount'];
                $arr1['CreateDate'] = $data['CreateDate'];
                $arr1['WorkDays'] = $data['WorkDays'];
                $arr1['CreditLevel'] = $data['CreditLevel'];
                $arr1['CanLoanAmount'] = $data['CanLoanAmount'];
                $arr1['HaveAmount'] = $data['HaveAmount'];
                $arr1['BusLis'] = $data['imgid1'];
                $res1 = $logisticsModel->where('UserId=' . $userModel->id)->select();
                if (count($res1) > 0) {
                    $result_2 = $logisticsModel->save($arr1, 'UserId=' . $userModel->id);
                } else {
                    $result_2 = $logisticsModel->save($arr1);
                }
                if ($result_1 !== false && $result_2 !== false) {
                    $this->success("操作成功", url("index"));
                } else {
                    $this->error("操作失败");
                }
            } else {
                $this->error("操作失败");
            }
        } else {
            $this->error("请求错误");
            return $this->fetch(':index');
        }
    }

    public function auditPost() {
        if ($this->request->isPost()) {
            $id = input('id');
            $data = input('post.');
            $result = false;
            $userModel = new UserModel();
            if (isset($data['audit'])) {
                $arr = array();
                if(!$data['CanLoanAmount']){
                    $this->error("请填写可贷款金额");
                }

                $arr["dk_status"] = 1;
                $result = $userModel->save($arr, ['id' => $id]);
            } elseif (isset($data['refuse'])) {
                $arr = array();
                $arr["dk_status"] = 2;
                $result = $userModel->save($arr, ['id' => $id]);
            }
            if ($result !== false) {
                $this->success("操作成功", url("index"));
            } else {
                $this->error("操作失败");
            }
        }
    }

}
